//
//  RegisterViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/20.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit

class RegisterViewController: BaseUIViewController {
    
    let countryField = LoginTextField()
    let phoneField = LoginTextField()
    
    let model: LoginModel
    let email: String
    
    init(model: LoginModel, email: String) {
        self.model = model
        self.email = email
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
    }
    
    func setupView() {
        self.navigationController?.navigationItem.title = "个人信息".localized
        
        let backgroundImage = UIImageView()
        backgroundImage.config
            .imageName("login_background")
            .addTo(view)
            .makeConstraints { (make) in
                make.left.top.right.equalTo(0)
                make.height.equalTo(backgroundImage.snp.width).multipliedBy(0.69)
        }
        
        let foregroundView = UIView().config
            .backgroundColor(.white)
            .cornerRadius(5)
            .shadowColor(.black)
            .shadowRadius(10)
            .shadowOffset(.zero)
            .shadowOpacity(0.1)
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(15)
                make.centerX.equalToSuperview()
                make.top.equalTo(backgroundImage.snp.bottom).offset(-70)
        }.view
        
        let titleLabel = UILabel().config
            .font(UIFont.systemFont(ofSize: 14, weight: .medium))
            .textColor(#colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1))
            .text("完善个人信息，赢取100积分奖励".localized)
            .addTo(foregroundView)
            .makeConstraints { (make) in
                make.top.equalTo(0)
                make.height.equalTo(60)
                make.centerX.equalToSuperview()
        }.view
        
        countryField.config
            .image(AppIcon.image("", size: 20, color: #colorLiteral(red: 0.8274509804, green: 0.8274509804, blue: 0.8274509804, alpha: 1)))
            .placeHolder("请选择国家".localized)
            .addTo(foregroundView)
            .extra{
                $0.textField.isUserInteractionEnabled = false
                $0.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(selectCountry)))
        }.makeConstraints { (make) in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(45)
        }
        
        phoneField.config
            .image(AppIcon.image("", size: 20, color: #colorLiteral(red: 0.8274509804, green: 0.8274509804, blue: 0.8274509804, alpha: 1)))
            .placeHolder("请输入电话".localized)
            .addTo(foregroundView)
            .makeConstraints { (make) in
                make.left.equalTo(20)
                make.right.equalTo(-20)
                make.top.equalTo(countryField.snp.bottom).offset(10)
                make.height.equalTo(45)
        }
        
        let emailField = LoginTextField().config
            .image(AppIcon.image("", size: 20, color: #colorLiteral(red: 0.8274509804, green: 0.8274509804, blue: 0.8274509804, alpha: 1)))
            .placeHolder("请输入邮箱".localized)
            .isUserInteractionEnabled(false)
            .addTo(foregroundView)
            .extra{
                $0.text = self.email
        }.makeConstraints { (make) in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(phoneField.snp.bottom).offset(10)
            make.height.equalTo(45)
        }.view
        
        UIButton().config
            .backgroundImage(UIImage.imageWithColor(APPConfig.baseColor))
            .clipsToBounds(true)
            .titleColor(.white)
            .cornerRadius(5)
            .addTarget(self, action: #selector(finishButtonClick))
            .title("下一步".localized)
            .font(UIFont.systemFont(ofSize: 16, weight: .medium))
            .addTo(foregroundView)
            .makeConstraints { (make) in
                make.top.equalTo(emailField.snp.bottom).offset(50)
                make.bottom.equalTo(-30)
                make.left.equalTo(20)
                make.height.equalTo(50)
                make.centerX.equalToSuperview()
        }
    }
    
    @objc func selectCountry() {
        let picker = CountrySelectView()
        picker.selectCountry = {
            self.countryField.text = $0
        }
        picker.showAnimation()
    }
    
    @objc func finishButtonClick() {
        for i in [countryField, phoneField] {
            if i.text.count == 0 {
                APPHud.shared.showError(status: i.placeHolder)
                return
            }
        }
        var model = RegisterModel()
        model.email = self.email
        model.country = self.countryField.text
        model.phone = self.phoneField.text
        let vc = Register2ViewController.init(model: self.model, registerModel: model)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .clearColor
    }
}
